package sjtu.ChatClient;

import java.util.HashMap;
import java.util.Iterator;

public class Util {
	/*
	 * returns the most frequently appeared value in the array
	 */
	public static int findMostFrequentValue(int [] values){
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		int i = 0;
		int j = 0;
		for(i = 0;i < values.length;i++){
			int count = 0;
			for(j = 0;j < values.length;j++){
				if(values[j] == values[i]){
					count++;
				}
			}
			
			map.put(values[i], count);
		}
		
		int maxNum = 0;
		int maxValue = values[0];
		
		Iterator itr = map.keySet().iterator();
		while(itr.hasNext()){
			Object key = itr.next();
			if(map.get(key) > maxNum){
				maxNum = map.get(key);
				maxValue = (Integer) key;
			}
		}
		
		return maxValue;
	}
}
